//
//  FinancialTableViewController.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/23.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "FinancialTableViewController.h"
#import "homeTableViewCell.h"
#import "DZQualityViewController.h"
@interface FinancialTableViewController () <financeCellDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (copy, nonatomic) NSString *cachePath;

@end

@implementation FinancialTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"投资理财"];
    self.tabBarItem.title = @"理财";
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self setTableView];
    [self getInfo];
   
}

- (void)setTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    _tableView.backgroundView = nil;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:homeTableViewCells bundle:nil] forCellReuseIdentifier:homeTableViewCells];
    self.tableView.mj_header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadForProductList)];
    [_tableView registerNib:[UINib nibWithNibName:@"homeTableViewCell" bundle:nil] forCellReuseIdentifier:@"homeCell"];
}

- (void)getInfo {
    _dataArray = [NSMutableArray array];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    _cachePath = [[paths firstObject]stringByAppendingString:@"/ListCache"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:_cachePath]) {
        NSData *data = [NSData dataWithContentsOfFile:_cachePath];
        if (data) {
            id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [self loadingData:json];
        }
    }
    [self loadForProductList];
}

- (void)loadForProductList {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Index/getProductList";
    request.httpMethod = @"GET";
    [DZNetAPI request:request success:^(id response) {
        [self.tableView.mj_header endRefreshing];
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            NSData *data = [NSJSONSerialization dataWithJSONObject:response options:NSJSONWritingPrettyPrinted error:nil];
            [data writeToFile:_cachePath atomically:YES];
            [_dataArray removeAllObjects];
            [self loadingData:response];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loadingData:(id)responseObject {
    NSArray *productList = responseObject[@"data"];
    for (NSDictionary *dic in productList) {
        _cellModel = [HomeCellModel mj_objectWithKeyValues:dic];
        [_dataArray addObject:_cellModel];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    homeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:homeTableViewCells];
    [cell cellReloadData:_dataArray[indexPath.section]];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 94;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCellModel *model = _dataArray[indexPath.section];
    if (model.remaining_investment_amount != 0) {
        [self buyFinance:model];
    }
}

#pragma mark ---- financeCellDelegate

- (void)buyFinance:(HomeCellModel *)model {
    DZQualityViewController *financialDetail = [[DZQualityViewController alloc] init];
    financialDetail.productId = [model.id integerValue];
    [financialDetail setupTitleView:model.name];
    financialDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:financialDetail animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

@end
